12345678910111213141516171819202122232425262728293031323334353637383940 |
- #include <stdio.h>
- #include <math.h>
- int is_prime(int num) {
- int i;
- if (num <= 1) {
- return 0;
- }
-
- for (i = 2; i <= sqrt(num); i++) {
- if (num % i == 0) {
- return 0;
- }
- }
-
- return 1;
- }
- int prime_sum(int lower, int upper) {
- int sum = 0;
- int i;
- for (i = lower; i <= upper; i++) {
- if (is_prime(i)) {
- sum += i;
- }
- }
-
- return sum;
- }
- int main() {
- int lower, upper;
- scanf("%d %d", &lower, &upper); // 输入区间的上下界
-
- int result = prime_sum(lower, upper);
-
- printf("%d-%d之间的素数和是%d\n", lower, upper, result);
-
- return 0;
- }
|